<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Point_model extends MY_Model
{
	private $table = 'blues_member_points';
    public function __construct() {
        parent::__construct();
    }
	/**
	 * 获取列表
	 *
	 * return array
	 */
	public function getPointList()
	{
		
    	$sql = "SELECT * FROM blues_point_carousel  ";
        $query = $this->db->query($sql);

		return $query->result_array();
	}
	public function addCarousel($data)
    {
        $res= $this->db->insert("blues_point_carousel",$data);
        if($res){
            return $this->db->insert_id();
        }
        return FALSE;
    }

	public function get_point_by_id($id)
    {
        $this->db->where("id",$id);
        $query=$this->db->get("blues_points_setting");
        return $query->row_array();
    }

	public function todayPointTotal($AccountID)
	{
		$todayTime = date("Y-m-d",time());
		$sql = "SELECT sum(points) cnt FROM {$this->table} where AccountID = '".$AccountID."' AND createtime = '".$todayTime."'";
		$query = $this->db->query($sql);
		$result =$query->row_array();
        return isset($result['cnt'])?$result['cnt']:0;
	}

	  
	
	
	
	
	
	public function addAnswerPoints($data = array()){
		//插入
		$res = $this->db->insert("blues_member_points",$data);
		if($res)
		{
			$this->totalPoint($data['AccountID'],$data['points']);
		}
		return $res;
    }
	
	public function addAnswerMoney($data = array()){
		//插入
		$res = $this->db->insert("blues_member_money",$data);
		if($res)
		{
			$this->totalMoney($data['AccountID'],$data['money']);
		}
		return $res;
    }
	
	public function totalPoint($AccountID,$toplus = 0){
		$sql = "SELECT points  FROM blues_user WHERE AccountID = '".$AccountID."' ";
		$query = $this->db->query($sql);
        $result =$query->row_array();
	
		$this->db->where(["AccountID"=>$AccountID]);
		$total = $result['points']+$toplus;
		$res = $this->db->update("blues_user",['points'=>$total]);
		return $res;
	}
	
	public function totalMoney($AccountID,$toplus = 0){
		$sql = "SELECT money  FROM blues_user WHERE AccountID = '".$AccountID."' ";
		$query = $this->db->query($sql);
        $result =$query->row_array();
	
		$this->db->where(["AccountID"=>$AccountID]);
		$total = $result['money']+$toplus;
		$res = $this->db->update("blues_user",['money'=>$total]);
		return $res;
	}
	
	public function addPoints($AccountID, $data = array()){
        //插入
        $this->db->where("AccountID", $AccountID);
        $res = $this->db->insert("blues_member_points",$data);
		$this->totalPoint($AccountID,$data['points']);
        return $res;
    }

	public function insertPoints($data = array()){
            //插入
        $res = $this->db->insert($this->table,$data);
        return $res;
    }
	
    public function setPoints($data = array()){
		$where = " ";
		if($data['typeid'])
		{
			$where .= " AND typeid={$data['typeid']}";
		}
        $sql = "SELECT sum(points) cnt FROM {$this->table} WHERE AccountID = '".$data['AccountID']."'  AND type = {$data['type']} {$where} ";
        $query = $this->db->query($sql);
        $result =$query->row_array();
        if(isset($result['cnt'])?$result['cnt']:0){
            return "exists";
        }else{
            //插入
            $res = $this->db->insert($this->table,$data);
			$this->totalPoint($data['AccountID'],$data['points']);
        }
		
        return $res;
    }

	public function getMyTimes($AccountID)
	{
		$cur_time =  date('Y-m-d',time());
		$sql = "SELECT count(*) cnt FROM blues_member_points WHERE AccountID = '{$AccountID}' AND type =9 AND createtime = '{$cur_time}'";
        $query = $this->db->query($sql);
        return  $query->row_array()['cnt'];
	}

    //签到
    public function signed($AccountID, $data = array()){
		
        $sql = "SELECT points, createtime FROM blues_member_points WHERE AccountID = '{$AccountID}' AND type =2 ORDER BY createtime DESC";
        $query = $this->db->query($sql);
        $result = $query->result_array();
        if(isset($result) && $result[0]['createtime'] == date('Y-m-d',time())){
            return "hassignin";
        }else{
            //插入
            $this->db->where("AccountID", $AccountID);
            $res = $this->db->insert("blues_member_points",$data);
			$this->totalPoint($AccountID,$data['points']);
        }
        return $res;
    }
	
	public function getPointByTime($time,$AccountID)
	{
		$this->db->where(["createtime"=>$time,'AccountID'=>$AccountID,'type'=>2]);
		$query = $this->db->get("blues_member_points");
		return $query->row_array();
	}
	
	public function toSendPoints($AccountID,$toAccountID,$points)
	{
		if($AccountID)
		{
			$sql = "UPDATE blues_user set points=(points-{$points}) WHERE AccountID = '{$AccountID}'";
			$query = $this->db->query($sql);
		}
		if($toAccountID)
		{
			$sql = "UPDATE blues_user set points=(points+{$points}) WHERE AccountID = '{$toAccountID}'";
			$query = $this->db->query($sql);
		}
		return $query;
		
	}

	public function getSendPointHistoryList($AccountID,$fields = " * ")
	{
		$sql = "SELECT {$fields} FROM blues_member_points where (AccountID = '{$AccountID}') AND  type =7 order by id desc";
		$query = $this->db->query($sql);
		return $query->result_array();
	}


}

